package server.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import server.demo.entity.RentalOrder;

import java.util.List;
import java.util.Optional;

@Repository
public interface RentalOrderRepository extends JpaRepository<RentalOrder, Long> {
    
    Optional<RentalOrder> findByOrderNo(String orderNo);
    
    List<RentalOrder> findByUserId(Long userId);
    
    List<RentalOrder> findByEquipmentId(Long equipmentId);
    
    List<RentalOrder> findByStatus(RentalOrder.OrderStatus status);
    
    @Query("SELECT COUNT(r) FROM RentalOrder r WHERE r.status = :status")
    Long countByStatus(RentalOrder.OrderStatus status);
    
    @Query("SELECT r FROM RentalOrder r WHERE r.userId = :userId ORDER BY r.createdAt DESC")
    List<RentalOrder> findByUserIdOrderByCreatedAtDesc(Long userId);
    
    void deleteByUserId(Long userId);
}